package com.zhuanzhuan.hero.user.enums;

import com.zhuanzhuan.hero.user.exception.BusinessException;
import com.zhuanzhuan.hero.user.util.Assert;
import lombok.Getter;

import java.util.Arrays;

/**
 * @ClassName StateEnum
 * @Description
 * @Author hanjunjun
 * @Date 2024/6/17 21:20
 */
@Getter
public enum StateEnum {
    INVALID(0, "无效的"),
    EFFECTIVE(1, "生效的"),
    IS_DELETED(2, "已删除")
    ;

    private final Integer code;

    private final String name;

    StateEnum(Integer code, String name) {
        this.code = code;
        this.name = name;
    }

    public static String getNameByCode(Integer code) {
        Assert.notNull(code, ExceptionMsgEnum.PARAM_ERROR);
        return Arrays.stream(StateEnum.values())
            .filter(anEnum -> code.equals(anEnum.getCode()))
            .findFirst()
            .orElseThrow(() -> new BusinessException("code不存在枚举"))
            .getName();
    }
}
